package com.tgh.utils;

import android.opengl.GLES20;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class GLCube {
    
    
    private int color=-1;
    private float angleX,angleY,angleZ;
    private boolean lighting;
    
    public GLCube() {
        float vertices[] = 
            {
                    -1.0f,  1.0f, 1.0f,
                     1.0f,  1.0f, 1.0f,
                     1.0f, -1.0f, 1.0f,
                    -1.0f, -1.0f, 1.0f,
                     
                    -1.0f,  1.0f,-1.0f,
                     1.0f,  1.0f,-1.0f,
                     1.0f, -1.0f,-1.0f,
                    -1.0f, -1.0f,-1.0f
            }; 

            byte maxColor=(byte)255;
            
            byte colors[] = 
            {
                    maxColor,maxColor,       0,maxColor,
                    0,       maxColor,maxColor,maxColor,
                    0,              0,       0,maxColor,
                    maxColor,       0,maxColor,maxColor,
                    
                    maxColor,       0,       0,maxColor,
                    0,       maxColor,       0,maxColor,
                    0,              0,maxColor,maxColor,
                    0,              0,       0,maxColor
            }; 


            byte indices[] =                  
                {
                    1,0,3,
                    1,3,2,
                    1,2,6,
                    1,6,5,
                    1,5,4,
                    1,4,0,
                    7,4,5,
                    7,5,6,
                    7,6,2,
                    7,2,3,
                    7,3,0,
                    7,0,4
                    
                };


            mFVertexBuffer=GLTools.makeFloatBuffer(vertices);
            mColorBuffer = GLTools.makeByteBuffer(colors);
            mIndicesBuffer = GLTools.makeByteBuffer(indices);
            
            
            float[] normals = GLTools.generateNormalsForTriangleList(vertices, indices);
            normalBuffer = GLTools.makeFloatBuffer(normals);
    }
    
    
    public void useDefaultColor() {
        this.color=-1;
    }


    
    public void draw() {
        GLES20.glVertexAttribPointer(GLShaderManager.GL_ATTRIBUTE_VERTEX, 3, GLES20.GL_FLOAT, false, 0, mFVertexBuffer);
        GLES20.glEnableVertexAttribArray(GLShaderManager.GL_ATTRIBUTE_VERTEX);
        
        GLES20.glVertexAttribPointer(GLShaderManager.GL_ATTRIBUTE_NORMAL, 3, GLES20.GL_FLOAT, false, 0, normalBuffer);
        GLES20.glEnableVertexAttribArray(GLShaderManager.GL_ATTRIBUTE_NORMAL);

        GLES20.glDrawElements( GL10.GL_TRIANGLES, 12 * 3, GL10.GL_UNSIGNED_BYTE, mIndicesBuffer);
    }
    
    private FloatBuffer mFVertexBuffer;
    private ByteBuffer  mColorBuffer;
    private ByteBuffer  mIndicesBuffer;
    private FloatBuffer normalBuffer;
}
